package com.nondo.dean.platform.controller.api.inner;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nondo.dean.platform.model.result.OrderUserResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.User;
import com.nondo.dean.platform.model.UserBean;
import com.nondo.dean.platform.service.UserService;

/**
 * 用户管理内部接口
 * @author zlj
 * @date 2018年4月17日
 */
@RestController("apiInnerUser")
@RequestMapping("/{v}/inner/user")
public class UserController {
    
    @Autowired
    UserService userService;
    
    /**
     * 查看用户详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<User> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<User> view = new View<User>(20000, "获取数据成功");
        
        User user = null;
        if (id != null) {
            user = userService.selectUser(id);
        } else {
            user = new User();
        }

        view.setData(user);
        return view;
    }
    
    /**
     * 查看用户详情
     * @param request
     * @param response
     * @param userName
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/get/{userName}")
    public View<User> getUserByUserName(HttpServletRequest request, HttpServletResponse response, @PathVariable("userName") String userName) {
        View<User> view = new View<User>(20000, "获取数据成功");
        
        User user = null;
        if (StringUtils.isNotEmpty(userName)) {
            user = userService.selectUserByName(userName);
        }

        view.setData(user);
        return view;
    }
    
    /**
     * 查看用户详情
     * @param request
     * @param response
     * @param mobile
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/search/{mobile}")
    public View<User> getUserByMobile(HttpServletRequest request, HttpServletResponse response, @PathVariable("mobile") String mobile) {
        View<User> view = new View<User>(20000, "获取数据成功");
        
        User user = null;
        if (StringUtils.isNotEmpty(mobile)) {
            user = userService.selectUserByMobile(mobile);
        }

        view.setData(user);
        return view;
    }
    
    /**
     * 按照用户名或者用户编码获取用户列表
     * @param request
     * @param response
     * @param user
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/search")
    public View<List<UserBean>> search(HttpServletRequest request, HttpServletResponse response, @RequestBody User user) {
        View<List<UserBean>> view = new View<List<UserBean>>(20000, "获取列表成功");

        List<UserBean> userList = userService.getUserList(user);
        
        view.setData(userList);
        
        return view;
    }
    
    /**
     * 更新店铺ID
     * @param request
     * @param response
     * @param userId 用户ID
     * @param storeId 店铺ID
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Integer> update(HttpServletRequest request, HttpServletResponse response, @RequestBody UserBean userBean) {
        View<Integer> view = new View<Integer>(20000, "获取数据成功");

        if (userBean != null) {
            User user = new User();
            user.setId(userBean.getId());
            user.setStoreId(userBean.getStoreId());
            int update = userService.updateUser(user);
            view.setData(update);
        } else {
            view.setData(0);
        }

        return view;
    }
    
    /**
     * 设置接单员
     * @param request
     * @param response
     * @param username
     * @param orderTakeUser
     * @return 影响行数
     */
    @ApiVersion(1)
    @RequestMapping("/update/taker")
    public View<Integer> updateOrderTakeUser(HttpServletRequest request, HttpServletResponse response,
        @RequestBody UserBean userBean) {
        View<Integer> view = new View<Integer>(20000, "获取数据成功");

        if (userBean != null) {
            if (StringUtils.isEmpty(userBean.getUserName())) {
                return View.ofParamsError("用户名不能为空");
            }
            User user = userService.selectUserByName(userBean.getUserName());
            user.setOrderTakeUser(userBean.getOrderTakeUser());
            int update = userService.updateUser(user);
            view.setData(update);
        } else {
            view.setData(0);
        }

        return view;
    }

    /**
     * 获取用户下的店铺Id
     * @param request
     * @param response
     * @param
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/store/id")
    public View<Integer> queryUserStoreId(HttpServletRequest request, HttpServletResponse response, @RequestBody Integer userId) {
        View<Integer> view = new View<Integer>(20000, "获取数据成功");

        Integer storeId=null;
        if (userId != null) {
            storeId= userService.queryStoreId(userId);
        }
        view.setData(storeId);
        return view;
    }

    /**
     * 按id查询user
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/select/{id}")
    public View<OrderUserResult> searchUser(HttpServletRequest request, HttpServletResponse response,@PathVariable("id") Integer id) {
        View<OrderUserResult> view = new View<OrderUserResult>(20000, "获取列表成功");

        OrderUserResult beans = userService.getUserResult(id);

        view.setData(beans);

        return view;
    }

}